package com.zzptc.lzh.myapp22;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    ListView lv_stus;
    List<Student>list;
    private static final String TAG = "Main2Activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        lv_stus=findViewById(R.id.lv_stus);
        list=new ArrayList<>();

        for (int i=0;i<100;i++){
            Student student=new Student(R.mipmap.ic_launcher,i,"姓名"+i,new Date()+"到校");
            list.add(student);
        }
        for (Student s:list){
            Log.d(TAG, s.getName());
        }
        MyAdapter myAdapter = new MyAdapter();//new它的适配器
        lv_stus.setAdapter(myAdapter);//绑定ListView

    }
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //获取数据
            Student student = list.get(position);
            //要做界面
            View view1 = View.inflate(Main2Activity.this, R.layout.item_stu, null);
            //要绑定数据了 先实例化
            ImageView iv_pic = view1.findViewById(R.id.iv_pic);
            iv_pic.setImageResource(R.mipmap.ic_launcher);
            TextView tv_id = view1.findViewById(R.id.tv_id);
            tv_id.setText(student.getId()+"");//将整数化为字符串
            TextView tv_name=view1.findViewById(R.id.tv_name);
            tv_name.setText(student.getName());
            TextView tv_time = view1.findViewById(R.id.tv_time);
            tv_time.setText(student.getTime());
            return view1;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.menu_login:
                    Toast.makeText(this,"你点击了我", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.menu_setting:
                    Toast.makeText(this,"你点击了我2", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.menu_exit:
                    Toast.makeText(this,"你点击了我3", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            return super.onOptionsItemSelected(item);
    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,1,0,"add登陆");
        menu.add(0,2,1,"add设置");
        menu.add(0,3,3,"add退出");
        return true;
    }*/

}


